package graphics.core;

import java.lang.StringBuilder;

import javax.vecmath.Point3f;
import javax.vecmath.Vector3f;
import javax.vecmath.Color3f;
import javax.vecmath.TexCoord2f;

/**
 * @author Joshua Sovanh Adams
 */
public class Vertex
{

	/**
	 *
	 */
	private Point3f position;

	/**
	 *
	 */
	private Vector3f normal;
	
	/**
	 *
	 */
	private Color3f color;

	/**
	 *
	 */	
	private TexCoord2f texCoord;

	/**
	 *
	 */
	public Vertex()
	{
		position = new Point3f();
		normal = new Vector3f();
		color = new Color3f();
		texCoord = new TexCoord2f();
	}

	/**
	 *
	 */
	public Vertex(Point3f position, Vector3f normal, Color3f color, TexCoord2f texCoord)
	{
		this.position = position;
		this.normal = normal;
		this.color = color;
		this.texCoord = texCoord;
	}

	/**
	 *
	 */
	public boolean equals(Vertex v1)
	{
		if(!this.position.equals(v1.position))
			return false;
		if(!this.normal.equals(v1.normal))
			return false;
		if(!this.color.equals(v1.color))
			return false;
		if(!this.texCoord.equals(v1.texCoord))
			return false;
		return true;
	}

	/**
	 *
	 */
	public boolean epsilonEquals(Vertex v1, Vertex v2, float epsilon) 
	{
		// TODO
		/*
          static bool equals(const vec3<T> & v1, const vec3<T> & v2, float epsilon)
            {
                if(fabs(v1[0]-v2[0]) > epsilon)
                    return false;
                if(fabs(v1[1]-v2[1]) > epsilon)
                    return false;
                if(fabs(v1[2]-v2[2]) > epsilon)
                    return false;
                return true;
            }
		*/

    	return false;
	}

	/**
	 * Linearly interpolated between this vertex and vertex1 and returns Vertex result.
	 * @param vertex1 the first vertex
	 * @param alpha the alpha interpolation parameter
	 * @return the interpolated Vertex
	 */
	public Vertex interpolate(Vertex v1, float alpha)
	{
		Point3f interpolatedPosition = new Point3f();
		Vector3f interpolatedNormal = new Vector3f();
		Color3f interpolatedColor = new Color3f();
		TexCoord2f interpolatedTexCoord = new TexCoord2f();

		interpolatedPosition.interpolate(this.position, v1.position, alpha);
		interpolatedNormal.interpolate(this.normal, v1.normal, alpha);
		interpolatedColor.interpolate(this.color, v1.color, alpha);
		interpolatedTexCoord.interpolate(this.texCoord, v1.texCoord, alpha);
	
		return new Vertex(interpolatedPosition, interpolatedNormal, interpolatedColor, interpolatedTexCoord);
	}

	/**
	 * Returns the a string with the values of this Vertex. It is in the
	 * form of [ (position.x, position.y, position.z), (normal.x, normal.y, normal.z), (color.x, color.y, color.z), (texCoord.x, texCoord.y) ].
	 * @return the string represention
	 */
	public String toString()
	{
		StringBuilder result = new StringBuilder();

		result.append("[ ");
		result.append(position.toString());
		result.append(", ");
		result.append(normal.toString());
		result.append(", ");
		result.append(color.toString());
		result.append(", ");
		result.append(texCoord.toString());
		result.append(" ]\n");

		return result.toString();
	}

	public static void main(String[] args)
	{
		Point3f point1 = new Point3f(0.0f, 0.0f, 0.0f);
		Point3f point2 = new Point3f(1.0f, 1.0f, 1.0f);
		Point3f result = new Point3f();
		
		result.interpolate(point1, point2, 0.5f);

		System.out.println(result);

		Vertex vertex1 = new Vertex(new Point3f(0.0f, 0.0f, 0.0f),
			new Vector3f(0.0f, 0.0f, 0.0f),
			new Color3f(0.0f, 0.0f, 0.0f),
			new TexCoord2f(0.0f, 0.0f));

		Vertex vertex2 = new Vertex(new Point3f(1.0f, 1.0f, 1.0f),
			new Vector3f(1.0f, 1.0f, 1.0f),
			new Color3f(1.0f, 1.0f, 1.0f),
			new TexCoord2f(1.0f, 1.0f));

		System.out.println(vertex1.toString());

		System.out.println(vertex2.toString());

		Vertex interpolatedVertex = vertex1.interpolate(vertex2, 0.5f);

		System.out.println(interpolatedVertex.toString());
	}
}